package cn.zyq.ykb.service.cps.impl;

import cn.zyq.ykb.system.common.BaseService;
import com.github.pagehelper.PageHelper;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.mapper.cps.CpsMapper;
import cn.zyq.ykb.beans.cps.Cps;
import cn.zyq.ykb.service.cps.CpsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.zyq.ykb.system.common.PageParam;
import com.github.pagehelper.PageInfo;


import java.time.LocalDateTime;
import java.util.UUID;
import java.util.List;

/**
 * cps-物料Service业务层处理
 *
 * @author SystemAuth
 * @date 2024-01-22
 */
@Service
public class CpsServiceImpl  extends BaseService implements CpsService {

    @Autowired
    private CpsMapper cpsMapper;

    /**
     * 查询cps-物料
     *
     * @param cpsID cps-物料主键
     * @return cps-物料
     */
    @Override
    public Cps selectCpsByCpsID(Integer cpsID) {
        Cps cps = cpsMapper.selectByPrimaryKey(cpsID);
        if (cps.getDeleted() == 1) {
            return null;
        }
        return cps;
    }

    /**
     * 查询cps-物料
     *
     * @param serviceID 业务ID
     * @return cps-物料
     */
    @Override
    public Cps selectCpsByServiceID(String serviceID) {
        Cps param = new Cps();
        param.setDeleted(0);
        param.setServiceID(serviceID);
        return this.cpsMapper.selectOne(param);
    }

    /**
     * 查询cps-物料列表
     *
     * @param pageParam
     * @return cps-物料
     */
    @Override
    public PageInfo<Cps> selectCpsList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        List<Cps> list = this.cpsMapper.list(pageParam.getKv());
        return new PageInfo<>(list);
    }

    /**
     * 查询cps-物料列表
     *
     * @param pageParam
     * @return cps-物料
     */
    @Override
    public List<Cps> selectCpsAllList(PageParam pageParam) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        pageParam.getKv().put("deleted", 0);
        return this.cpsMapper.list(pageParam.getKv());
    }
    /**
     * 新增cps-物料
     *
     * @param param cps-物料
     * @return 结果
     */
    @Override
    public int insertCps(Cps param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.cpsMapper.insertSelective(param);
    }

    /**
     * 修改cps-物料
     *
     * @param param cps-物料
     * @return 结果
     */
    @Override
    public int updateCps(Cps param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.cpsMapper.updateByPrimaryKeySelective(param);
    }

    /**
     * 批量删除cps-物料
     *
     * @param cpsIDs 需要删除的cps-物料主键
     * @return 结果
     */
    @Override
    public int deleteCpsByCpsIDs(Integer[] cpsIDs) {
        int i = 0;
        for (Integer cpsID : cpsIDs) {
            i += this.deleteCpsByCpsID(cpsID);
        }
        return i;
    }

    /**
     * 删除cps-物料信息
     *
     * @param cpsID cps-物料主键
     * @return 结果
     */
    @Override
    public int deleteCpsByCpsID(Integer cpsID) {
        Cps cps = this.selectCpsByCpsID(cpsID);
        if (cps == null) {
            return 0;
        } else {
            Cps storeInfo = new Cps();
            storeInfo.setCpsID(cpsID);
            storeInfo.setDeleted(1);
            storeInfo.setUpdateBy(SecurityUtils.getSysAdminID());
            storeInfo.setUpdateTime(LocalDateTime.now());
            this.cpsMapper.updateByPrimaryKeySelective(storeInfo);
            return 1;
        }
    }
}
